import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'

// console
import vConsolePlugin from 'vite-plugin-simple-vconsole'
// gzip插件
import viteCompression from 'vite-plugin-compression'
// CDN 加速
// import viteCDNPlugin from 'vite-plugin-cdn-import'
// 自动导入
import AutoImport from 'unplugin-auto-import/vite'
import eslintPlugin from 'vite-plugin-eslint'
import { visualizer } from 'rollup-plugin-visualizer';

// eslint-disable-next-line no-undef
const resolve = (dir) => path.resolve(__dirname, dir)
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  // eslint-disable-next-line no-undef
  const env = loadEnv(mode, process.cwd()) // 获取当前环境下的.env文件内容
  console.log('🏆 ~ env:', env)
  return {
    base: './',
    publicDir: resolve('public'), //静态资源服务的文件夹
    plugins: [
      visualizer(),
      vConsolePlugin({
        enable: false
      }),
      vue(),
      eslintPlugin({
        include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
      }),
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz'
      }),
      Components({
        resolvers: [VantResolver()]
      }),
      AutoImport({
        imports: [
          'vue',
          'vue-router',
          {
            '@/utils': [['default', '$utils']]
          }
        ],
        ignore: ['h'],
        dts: 'src/auto-import.d.ts'
      }),
      {
        name: 'bundle-time', // 打包时添加打包时间
        apply: 'build',
        enforce: 'post',
        transformIndexHtml: () => {
          return [
            {
              tag: 'script',
              children: `console.info("bundle time: %s", '${new Date().toLocaleString()}')`,
              injectTo: 'body'
            }
          ]
        }
      }
    ], // 配置别名
    resolve: {
      alias: {
        '@': resolve('src')
      }, // 导入时想要省略的扩展名列表
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    //启动服务配置
    server: {
      host: '0.0.0.0',
      port: 3010,
      open: true, // 自动在浏览器打开
      proxy: {
        '^/api': {
          // target: 'http://192.168.1.223:8000',
          // target: 'http://192.168.3.138:9001',
          target: 'http://mx-xx.test.ixunhang.com',
          // target: 'http://mx.test.ixunhang.com',

          changeOrigin: true,
          rewrite: (path) => path
        },
        '^/daynamicBriefReport': {
          // target: "http://192.168.1.223:9002",
          target: 'http://10.8.0.26:8000',
          // target: "http://223.108.29.94:8000",
          // target: "https://rj-aa.ixunhang.com",
          changeOrigin: true,
          rewrite: (path) => path
        }
      }
    }, // 打包配置
    build: {
      //浏览器兼容性  "esnext"|"modules"
      target: 'modules', //指定输出路径
      outDir: 'h5', //生成静态资源的存放路径
      cssCodeSplit: true,
      assetsDir: 'assets', //启用/禁用 CSS 代码拆分
      sourcemap: false,
      assetsInlineLimit: 10240,
      minify: 'terser',
      terserOptions: { // 打包环境移除console.log, debugger
        compress: {
          drop_console: false,
          drop_debugger: true
        }
      },
      rollupOptions: {
        input: {
          main: resolve("index.html"),
        },
        output: {
          // entryFileNames: `js/[name]-[hash].js`,
          // chunkFileNames: `js/[name]-[hash].js`,
          // assetFileNames: `[ext]/[name]-[hash].[ext]`,
          manualChunks: id => {
            // 将 node_modules 中的代码单独打包成一个 JS 文件
            if (id.includes('node_modules')) {
              return 'vendor'
            }
          }
        },
      },
    }
  }
})
